/*     */ package org.poebot.hal;
/*     */ 
/*     */ import java.awt.Dimension;
/*     */ import java.awt.GridLayout;
/*     */ import java.awt.event.ActionEvent;
/*     */ import java.awt.event.ActionListener;
/*     */ import java.io.File;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
///*     */ import java.util.ArrayList;
/*     */ import javax.swing.BoxLayout;
///*     */ import javax.swing.ButtonModel;
/*     */ import javax.swing.DefaultListCellRenderer;
/*     */ import javax.swing.DefaultListModel;
/*     */ import javax.swing.JButton;
/*     */ import javax.swing.JFileChooser;
/*     */ import javax.swing.JFrame;
/*     */ import javax.swing.JLabel;
/*     */ import javax.swing.JList;
/*     */ import javax.swing.JPanel;
/*     */ import javax.swing.JScrollPane;
/*     */ import javax.swing.JTabbedPane;
/*     */ import javax.swing.JTextArea;
/*     */ import javax.swing.JTextField;
/*     */ import javax.swing.JToggleButton;
/*     */ import javax.swing.border.TitledBorder;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.apache.log4j.PropertyConfigurator;
import org.poebot.hal.BotPrompter;
import org.poebot.hal.BotTrainer;
import org.poebot.hal.Chatbot;
import org.poebot.hal.ChatbotListener;
/*     */ 
/*     */ public class PoeBotApp extends JFrame
/*     */   implements ActionListener, ChatbotListener
/*     */ {
/*     */   private static final long serialVersionUID = -7748745528289585256L;
/* 477 */   private Chatbot bot = null;
/*     */ 
/* 479 */   private JButton loadBrainButton = null;
/* 480 */   private JButton trainBrainButton = null;
/* 481 */   private JButton sayButton = null;
/* 482 */   private JTextArea inputField = null;
/* 483 */   private JTextArea outputField = null;
/*     */ 
/* 485 */   private JTextField serverAddressField = null;
/* 486 */   private JTextField serverChannelField = null;
/* 487 */   private JTextField botNickField = null;
/* 488 */   private JButton connectButton = null;
/*     */   private ChatTextArea chatText;
/* 491 */   private JToggleButton logButton = null;
/* 492 */   private JButton logFileButton = null;
/* 493 */   private boolean logEnabled = true;
/*     */ 
/* 495 */   private JToggleButton learnFromChatButton = null;
/* 496 */   private JToggleButton speakRandomlyButton = null;
/* 497 */   private JTextField speechDelayField = null;
/*     */ 
/* 499 */   private JList ignoreList = null;
/* 500 */   private JButton addNameButton = null;
/* 501 */   private JButton removeNameButton = null;
/* 502 */   private JTextField ignoreNameField = null;
///* 503 */   private ArrayList<String> ignoreNames = new ArrayList<String>();
/*     */ 
/* 506 */   private File logFile = null;
/* 507 */   private FileWriter logOut = null;
/* 508 */   private Object logLock = new int[1];
/*     */ 
/* 510 */   private BotPrompter botPrompter = null;
/*     */ 
/* 512 */   private static Logger logger = Logger.getLogger(PoeBotApp.class);
/*     */ 
/*     */   public PoeBotApp()
/*     */   {
/*  42 */     super("#poe-news on Rizon ChatterBot 2.0 - Resurrected by Leonassan");
/*  43 */     initFrame();
/*     */ 
/*  45 */     this.bot = new Chatbot();
/*  46 */     this.bot.addChatListener(this);
/*  47 */     synchronized (this.logLock)
/*     */     {
/*  49 */       this.logFile = new File("chatlog.log");
/*  50 */       if (!this.logFile.exists())
/*     */         try {
/*  52 */           this.logFile.createNewFile();
/*     */         } catch (IOException e) {
/*  54 */           logger.error(e);
/*     */         }
/*     */     }
/*     */     try {
/*  58 */       synchronized (this.logLock)
/*     */       {
/*  60 */         this.logOut = new FileWriter(this.logFile, true);
/*     */       }
/*     */     } catch (Exception e) {
/*  63 */       logger.error(e);
/*     */     }
/*     */   }
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/*  72 */     PropertyConfigurator.configure("log4j.config");
/*  73 */     PoeBotApp pbbot1 = new PoeBotApp();
/*  74 */     pbbot1.setDefaultCloseOperation(3);
/*  75 */     pbbot1.setSize(600, 450);
/*  76 */     pbbot1.setVisible(true);
/*     */   }
/*     */ 
/*     */   public void actionPerformed(ActionEvent event)
/*     */   {
/*  82 */     Object source = event.getSource();
/*  83 */     if (source == this.connectButton)
/*     */     {
/*  85 */       this.bot.setSever(this.serverAddressField.getText());
/*  86 */       this.bot.setChannel(this.serverChannelField.getText());
/*  87 */       this.bot.setNick(this.botNickField.getText());
/*  88 */       if (this.bot.isAlive())
/*  89 */         this.bot.setChangeServer(true);
/*     */       else
/*  91 */         this.bot.start();
/*     */     }
/*  93 */     else if (source == this.loadBrainButton)
/*     */     {
/*  95 */       JFileChooser chooser = new JFileChooser();
/*  96 */       chooser.setMultiSelectionEnabled(false);
/*  97 */       int choice = chooser.showOpenDialog(this);
/*  98 */       if (choice == 0)
/*     */       {
/* 100 */         this.bot.loadBrain(chooser.getSelectedFile());
/*     */       }
/*     */     }
/* 103 */     else if (source == this.logButton)
/*     */     {
/* 105 */       if (this.logButton.getModel().isSelected())
/*     */       {
/* 107 */         this.logButton.setText("Logging Enabled");
/* 108 */         this.logEnabled = true;
/*     */       }
/*     */       else
/*     */       {
/* 112 */         this.logButton.setText("Logging Disabled");
/* 113 */         this.logEnabled = false;
/*     */       }
/*     */     }
/* 116 */     else if (source == this.logFileButton)
/*     */     {
/* 118 */       JFileChooser chooser = new JFileChooser();
/* 119 */       chooser.setMultiSelectionEnabled(false);
/* 120 */       int choice = chooser.showOpenDialog(this);
/* 121 */       if (choice == 0)
/*     */       {
/* 123 */         synchronized (this.logLock)
/*     */         {
/* 125 */           this.logFile = chooser.getSelectedFile();
/* 126 */           if (!this.logFile.exists())
/*     */             try {
/* 128 */               this.logFile.createNewFile();
/*     */             } catch (IOException e) {
/* 130 */               e.printStackTrace();
/*     */             }
/*     */         }
/*     */         try {
/* 134 */           synchronized (this.logLock)
/*     */           {
/* 136 */             this.logOut = new FileWriter(this.logFile, true);
/*     */           }
/*     */         } catch (Exception e) {
/* 139 */           logger.error(e);
/*     */         }
/*     */       }
/*     */ 
/*     */     }
/* 144 */     else if (source == this.sayButton)
/*     */     {
/* 146 */       String text = this.inputField.getText();
/* 147 */       String reply = this.bot.getSentence(text);
/* 148 */       this.outputField.setText(reply);
/* 149 */       this.outputField.updateUI();
/*     */     }
/* 151 */     else if (source == this.trainBrainButton)
/*     */     {
/* 153 */       loadTrainingFile();
/*     */     }
/* 155 */     else if (source == this.speakRandomlyButton)
/*     */     {
/* 157 */       if (this.speakRandomlyButton.getModel().isSelected())
/*     */       {
/* 159 */         this.speakRandomlyButton.setText("Speak At Will");
/* 160 */         this.bot.setSpeakAtWill(true);
/*     */         try {
/* 162 */           long delay = Long.parseLong(this.speechDelayField.getText()) * 1000L;
/* 163 */           if (delay <= 0L) return;
/* 165 */           this.bot.setSpeakingDelay(delay);
/* 166 */           this.botPrompter = new BotPrompter(this.bot, delay);
/* 167 */           this.botPrompter.start();
/*     */         }
/*     */         catch (NumberFormatException localNumberFormatException)
/*     */         {
/*     */         }
/*     */ 
/*     */       }
/*     */       else
/*     */       {
/* 177 */         this.speakRandomlyButton.setText("Speak When Spoken To");
/* 178 */         this.bot.setSpeakAtWill(false);
/* 179 */         if (this.botPrompter != null)
/*     */         {
/* 181 */           this.botPrompter.interrupt();
/* 182 */           this.botPrompter = null;
/*     */         }
/*     */       }
/*     */     }
/* 186 */     else if (source == this.speechDelayField)
/*     */     {
/*     */       try {
/* 189 */         long delay = Long.parseLong(this.speechDelayField.getText()) * 1000L;
/* 190 */         this.bot.setSpeakingDelay(delay);
/* 191 */         if (this.botPrompter != null)
/*     */         {
/* 193 */           this.botPrompter.interrupt();
/*     */         }
/* 195 */         if (this.speakRandomlyButton.getModel().isSelected())
/*     */         {
/* 197 */           this.botPrompter = new BotPrompter(this.bot, delay);
/* 198 */           this.botPrompter.start(); return;
/*     */         }
/*     */ 
/* 202 */         this.botPrompter = null;
/*     */       }
/*     */       catch (NumberFormatException localNumberFormatException1)
/*     */       {
/*     */       }
/*     */ 
/*     */     }
/* 210 */     else if (source == this.learnFromChatButton)
/*     */     {
/* 212 */       if (this.learnFromChatButton.getModel().isSelected())
/*     */       {
/* 214 */         this.learnFromChatButton.setText("Learn From Chat");
/* 215 */         this.bot.setLearnFromChat(true);
/*     */       }
/*     */       else
/*     */       {
/* 219 */         this.learnFromChatButton.setText("Don't Learn From Chat");
/* 220 */         this.bot.setLearnFromChat(false);
/*     */       }
/*     */     }
/* 223 */     else if (source == this.addNameButton)
/*     */     {
/* 225 */       if (this.bot.addNameToIgnoreList(this.ignoreNameField.getText()))
/* 226 */         ((DefaultListModel)this.ignoreList.getModel()).addElement(this.ignoreNameField.getText());
/*     */     }
/* 228 */     else if (source == this.removeNameButton)
/*     */     {
/* 230 */       this.bot.removeNameFromIgnoreList(this.ignoreNameField.getText());
/* 231 */       ((DefaultListModel)this.ignoreList.getModel()).removeElement(this.ignoreNameField.getText());
/*     */     }
/*     */   }
/*     */ 
/*     */   public void newLineReceived(String line)
/*     */   {
/* 238 */     this.chatText.addLine(line);
/*     */   }
/*     */ 
/*     */   public void trainedOnLine(String line)
/*     */   {
/* 243 */     if (this.logEnabled)
/*     */     {
/* 245 */       if (this.logOut != null)
/*     */         try {
/* 247 */           synchronized (this.logLock)
/*     */           {
/* 249 */             this.logOut.write(line + '\n');
/* 250 */             this.logOut.flush();
/*     */           }
/*     */         } catch (IOException e) {
/* 253 */           logger.error(e);
/*     */         }
/*     */     }
/*     */   }
/*     */ 
/*     */   private void initFrame()
/*     */   {
/* 263 */     this.loadBrainButton = new JButton("Load Brain");
/* 264 */     this.loadBrainButton.addActionListener(this);
/*     */ 
/* 266 */     this.trainBrainButton = new JButton("Train from text");
/* 267 */     this.trainBrainButton.addActionListener(this);
/*     */ 
/* 269 */     this.sayButton = new JButton("Say");
/* 270 */     this.sayButton.addActionListener(this);
/* 271 */     this.sayButton.setMaximumSize(new Dimension(150, 20));
/*     */ 
/* 273 */     this.inputField = new JTextArea();
/* 274 */     this.inputField.setWrapStyleWord(true);
/* 275 */     this.inputField.setLineWrap(true);
/*     */ 
/* 277 */     this.outputField = new JTextArea();
/* 278 */     this.outputField.setEditable(false);
/* 279 */     this.outputField.setWrapStyleWord(true);
/* 280 */     this.outputField.setLineWrap(true);
/*     */ 
/* 283 */     JPanel localPanel = new JPanel();
/* 284 */     localPanel.setLayout(new BoxLayout(localPanel, 1));
/*     */ 
/* 286 */     JScrollPane outPane = new JScrollPane(this.outputField);
/* 287 */     outPane.setBorder(new TitledBorder("Output"));
/* 288 */     localPanel.add(outPane);
/*     */ 
/* 290 */     JPanel inputPanel = new JPanel();
/* 291 */     inputPanel.setLayout(new BoxLayout(inputPanel, 0));
/* 292 */     JScrollPane inPane = new JScrollPane(this.inputField);
/* 293 */     inPane.setBorder(new TitledBorder("Input"));
/* 294 */     inputPanel.add(inPane);
/* 295 */     inputPanel.add(this.sayButton);
/*     */ 
/* 297 */     localPanel.add(inputPanel);
/*     */ 
/* 299 */     JPanel brainPanel = new JPanel(new GridLayout(1, 2));
/* 300 */     brainPanel.add(this.loadBrainButton);
/* 301 */     brainPanel.add(this.trainBrainButton);
/* 302 */     brainPanel.setMaximumSize(new Dimension(300, 20));
/*     */ 
/* 305 */     localPanel.add(brainPanel);
/*     */ 
/* 309 */     JTabbedPane tabbedPane = new JTabbedPane();
/*     */ 
/* 311 */     tabbedPane.add("Local + Training", localPanel);
/*     */ 
/* 314 */     JPanel chatPanel = new JPanel();
/* 315 */     chatPanel.setLayout(new BoxLayout(chatPanel, 0));
/*     */ 
/* 318 */     JPanel chatConnectionPanel = new JPanel();
/* 319 */     chatConnectionPanel.setLayout(new BoxLayout(chatConnectionPanel, 1));
/*     */ 
/* 321 */     this.serverAddressField = new JTextField();
/* 322 */     this.serverAddressField.setBorder(new TitledBorder("Server Address"));
/* 323 */     this.serverAddressField.setText("irc.gamepad.ca");
/*     */ 
/* 325 */     this.serverChannelField = new JTextField();
/* 326 */     this.serverChannelField.setBorder(new TitledBorder("Server Channel"));
/* 327 */     this.serverChannelField.setText("#");
/*     */ 
/* 329 */     this.botNickField = new JTextField();
/* 330 */     this.botNickField.setBorder(new TitledBorder("Bot Nick"));
/* 331 */     this.botNickField.setName("PoEChatterBot");
/*     */ 
/* 333 */     this.connectButton = new JButton("Connect");
/* 334 */     this.connectButton.addActionListener(this);
/*     */ 
/* 336 */     JPanel fieldPanel = new JPanel(new GridLayout(1, 4));
/*     */ 
/* 338 */     fieldPanel.add(this.serverAddressField);
/* 339 */     fieldPanel.add(this.serverChannelField);
/* 340 */     fieldPanel.add(this.botNickField);
/* 341 */     fieldPanel.add(this.connectButton);
/* 342 */     fieldPanel.setMaximumSize(new Dimension(450, 20));
/*     */ 
/* 344 */     chatConnectionPanel.add(fieldPanel);
/*     */ 
/* 346 */     this.chatText = new ChatTextArea(100);
/* 347 */     this.chatText.setWrapStyleWord(true);
/* 348 */     this.chatText.setLineWrap(true);
/* 349 */     this.chatText.setEditable(false);
/* 350 */     JScrollPane chatScroll = new JScrollPane(this.chatText);
/* 351 */     chatConnectionPanel.add(chatScroll);
/*     */ 
/* 353 */     this.logButton = new JToggleButton("Logging Enabled", true);
/* 354 */     this.logButton.addActionListener(this);
/*     */ 
/* 356 */     this.logFileButton = new JButton("Choose log...");
/* 357 */     this.logFileButton.addActionListener(this);
/*     */ 
/* 359 */     JPanel controlPanel = new JPanel(new GridLayout(1, 2));
/* 360 */     controlPanel.add(this.logButton);
/* 361 */     controlPanel.add(this.logFileButton);
/* 362 */     controlPanel.setMaximumSize(new Dimension(300, 20));
/* 363 */     chatConnectionPanel.add(controlPanel);
/*     */ 
/* 366 */     JPanel chatOptionsPanel = new JPanel();
/* 367 */     chatOptionsPanel.setBorder(new TitledBorder("Options"));
/* 368 */     chatOptionsPanel.setLayout(new BoxLayout(chatOptionsPanel, 1));
/*     */ 
/* 370 */     this.learnFromChatButton = new JToggleButton("Bot Learns From Chat", true);
/* 371 */     this.learnFromChatButton.addActionListener(this);
/* 372 */     chatOptionsPanel.add(this.learnFromChatButton);
/*     */ 
/* 375 */     this.speakRandomlyButton = new JToggleButton("Speak When Spoken To", false);
/* 376 */     this.speakRandomlyButton.addActionListener(this);
/* 377 */     chatOptionsPanel.add(this.speakRandomlyButton);
/*     */ 
/* 379 */     JPanel delayPanel = new JPanel();
/* 380 */     delayPanel.setLayout(new BoxLayout(delayPanel, 0));
/* 381 */     JLabel label1 = new JLabel("Bot talks every ");
/* 382 */     delayPanel.add(label1);
/* 383 */     this.speechDelayField = new JTextField();
/* 384 */     this.speechDelayField.setMaximumSize(new Dimension(80, 20));
/* 385 */     this.speechDelayField.addActionListener(this);
/* 386 */     delayPanel.add(this.speechDelayField);
/* 387 */     JLabel label2 = new JLabel(" seconds.");
/* 388 */     delayPanel.add(label2);
/*     */ 
/* 390 */     chatOptionsPanel.add(delayPanel);
/*     */ 
/* 393 */     JPanel ignorePanel = new JPanel();
/* 394 */     ignorePanel.setBorder(new TitledBorder("Ignore list"));
/* 395 */     ignorePanel.setLayout(new BoxLayout(ignorePanel, 1));
/*     */ 
/* 397 */     this.ignoreList = new JList();
/* 398 */     this.ignoreList.setModel(new DefaultListModel());
/* 399 */     this.ignoreList.setCellRenderer(new DefaultListCellRenderer());
/*     */ 
/* 401 */     JScrollPane ignoreListPane = new JScrollPane(this.ignoreList);
/* 402 */     ignoreListPane.setMaximumSize(new Dimension(200, 400));
/* 403 */     ignorePanel.add(ignoreListPane);
/*     */ 
/* 406 */     this.ignoreNameField = new JTextField();
/* 407 */     this.ignoreNameField.setMaximumSize(new Dimension(200, 20));
/* 408 */     ignorePanel.add(this.ignoreNameField);
/*     */ 
/* 410 */     JPanel ignoreButtonsPanel = new JPanel();
/* 411 */     ignoreButtonsPanel.setLayout(new GridLayout(1, 2));
/* 412 */     ignoreButtonsPanel.setMaximumSize(new Dimension(200, 20));
/*     */ 
/* 414 */     this.addNameButton = new JButton("Add");
/* 415 */     this.addNameButton.addActionListener(this);
/* 416 */     ignoreButtonsPanel.add(this.addNameButton);
/*     */ 
/* 418 */     this.removeNameButton = new JButton("Remove");
/* 419 */     this.removeNameButton.addActionListener(this);
/* 420 */     ignoreButtonsPanel.add(this.removeNameButton);
/*     */ 
/* 422 */     ignorePanel.add(ignoreButtonsPanel);
/*     */ 
/* 424 */     chatOptionsPanel.add(ignorePanel);
/*     */ 
/* 426 */     chatPanel.add(chatConnectionPanel);
/* 427 */     chatPanel.add(chatOptionsPanel);
/*     */ 
/* 431 */     tabbedPane.add("IRC", chatPanel);
/*     */ 
/* 434 */     add(tabbedPane);
/*     */   }
/*     */ 
/*     */   private void loadTrainingFile()
/*     */   {
/* 446 */     JFileChooser chooser = new JFileChooser();
/* 447 */     chooser.setMultiSelectionEnabled(false);
/* 448 */     TrainingFileAccessory trainingAccessory = new TrainingFileAccessory();
/* 449 */     chooser.setAccessory(trainingAccessory);
/* 450 */     int choice = chooser.showOpenDialog(this);
/* 451 */     if (choice == 0)
/*     */     {
/* 453 */       File trainingFile = chooser.getSelectedFile();
/* 454 */       if (trainingFile.equals(this.logFile))
/*     */       {
/* 456 */         logger.debug("Training on log file!");
/* 457 */         trainingFile = this.logFile;
/*     */       }
/*     */ 
/* 460 */       if (trainingAccessory.isLineParsingSelected())
/*     */       {
/* 462 */         BotTrainer trainer = new BotTrainer(this.bot, trainingFile, 3523);
/* 463 */         new TrainingProgressWindow(this, trainer);
/* 464 */         trainer.start();
/*     */       }
/*     */       else
/*     */       {
/* 469 */         BotTrainer trainer = new BotTrainer(this.bot, trainingFile, 4123);
/* 470 */         new TrainingProgressWindow(this, trainer);
/* 471 */         trainer.start();
/*     */       }
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\workspace\dognuts\dognuts\dognuts.jar
 * Qualified Name:     org.dognuts.gui.DognutsApp
 * JD-Core Version:    0.6.0
 */